﻿package com.dispatcher;

import com.dispatcher.helper.SDHelper;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class HomeTabActivity extends TabActivity implements OnTabChangeListener {
	// Tab id
    private static final String TAB_UNASSIGNED = "unassigned";
    private static final String TAB_MIME = "mime";
    private static final String TAB_sended = "sended";
	private TabHost mTabHost;
	private Intent it;
	public static boolean isShowDialog = true;
	GlobalVar glVar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_tab_activity);
		it = getIntent();
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		initView();
		startService(new Intent("com.dispatcher.LocalService"));
	}
	
	private void initView() {
		TextView presentid=(TextView)findViewById(R.id.presentid);
		presentid.setText("我的代号:"+SDHelper.getPresentId());
		
		TextView leftView = new TextView(this);
		TextView rightView = new TextView(this);
		TextView sendedView = new TextView(this);
		
		leftView.setText("未分配");
		leftView.setTextColor(Color.BLACK);
		rightView.setText("给我的");
		rightView.setTextColor(Color.BLACK);
		
		sendedView.setText("已发货");
		sendedView.setTextColor(Color.BLACK);
		
		leftView.setBackgroundResource(R.drawable.btn_yellow_rect_selector);
		rightView.setBackgroundResource(R.drawable.btn_yellow_rect_selector);
		sendedView.setBackgroundResource(R.drawable.btn_yellow_rect_selector);
		
		leftView.setSelected(true);
		
		leftView.setGravity(Gravity.CENTER);
		rightView.setGravity(Gravity.CENTER);
		sendedView.setGravity(Gravity.CENTER);

		mTabHost = (TabHost) this.findViewById(android.R.id.tabhost);
		mTabHost.setup();
		
		Intent i = new Intent(this, OrderActivity.class);
		TabSpec tab1 = mTabHost
				.newTabSpec(TAB_UNASSIGNED)
				.setIndicator(leftView)
				.setContent(i);
		mTabHost.addTab(tab1);
		Intent my=new Intent(this, MyOrderActivity.class);
		my.putExtra("task", "getMyPickOrder");
		TabSpec tab2 = mTabHost
				.newTabSpec(TAB_MIME)
				.setIndicator(rightView)
				.setContent(my);
		mTabHost.addTab(tab2);
		
		Intent send=new Intent(this, MyOrderActivity.class);
		send.putExtra("task", "getSendedOrder");
		TabSpec tab3 = mTabHost
				.newTabSpec(TAB_sended)
				.setIndicator(sendedView)
				.setContent(send);
		mTabHost.addTab(tab3);

		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTabByTag(TAB_UNASSIGNED);
		
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		if(it != null) {
			boolean b = it.getBooleanExtra("backtohome", false);
			if(b) {
				mTabHost.setCurrentTabByTag(TAB_MIME);
			}
		}
	}
	
	@Override
	public void onTabChanged(String tabId) {
		getTabHost().setCurrentTabByTag(tabId);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		finish();
		return false;
	}
	
}
